import Taro from "@tarojs/taro";
import config from './baseUrl'
const BASE_URL = config.baseUrl
export default function requset( url, option ) {
    return new Promise((resolve, reject) => {
        Taro.request({
            url: BASE_URL + url,
            method: option.method.toUpperCase() || "GET",
            header: {
                // "Content-type": option.paramsFormData || "application/json"
                ...option.header,
            },
            data: option.data || {},
            success: (result) => {
                console.log('request全部数据：', result)
                const { data } = result;
                if( data.code === 403 ) {
                    Taro.removeStorage({key: 'token'});
                    Taro.removeStorage({key: 'userId'});
                    Taro.removeStorage({key: 'phone'});
                    Taro.showToast({
                        icon: 'error',
                        title: '登录失效'
                    });
                    setTimeout(() => {
                        Taro.redirectTo({
                            url: '/pages/index/index'
                        })
                    },1000)
                } else {
                    resolve(data)
                    // resolve('走成功的回调')
                }
            },
            fail: async (error) => {
                console.error('请求失败：', error);
                // 获取网络状态
                const networkType = await Taro.getNetworkType();
                if (networkType.networkType === 'none') {
                    Taro.showToast({
                        icon: 'none',
                        title: '当前无网络，请检查网络连接',
                        duration: 3000,
                    });
                } else {
                    Taro.showToast({
                        icon: 'none',
                        title: '请求失败，请稍后重试',
                        duration: 3000,
                    });
                }
                reject('此次请求失败：', error)
            }
        })
    })
}